package com.itheima.controller.permission;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Permission;
import com.itheima.utils.BeanUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/permission/*")
public class PermissionServlet extends BaseServlet {

    /**
     * 查询所有权限信息并分页
     * @param req
     * @param resp
     * @throws Exception
     */
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        Page<Permission> page = null;
        //获取前端传递的当前页面
        String pageNum = req.getParameter("pageNum");
        //获取前端传递的每页条目数
        String pageSize = req.getParameter("pageSize");
        String name = req.getParameter("name");
        if(name!=null){
            page = permissionService.findByName(pageNum,pageSize,name);
        }else {
            page = permissionService.findAll(pageNum,pageSize);
        }
        PageInfo<Permission> pageInfo = new PageInfo<Permission>(page);
        //获取分页信息
        System.out.println(pageInfo);
        //存入作用域
        req.setAttribute("page",pageInfo);
        //实现页面跳转
        req.getRequestDispatcher("/pages/permission/permission-list.jsp").forward(req,resp);
    }

    /**
     * 新增权限信息
     * @param req
     * @param resp
     * @throws Exception
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //过工具类获取页面数据封装到指定对象中
        Permission permission = BeanUtil.fillBean(req, Permission.class);
        //实现方法
        permissionService.save(permission);
        //调用findAll
        findAll(req,resp);
    }

    /**
     * 根据id删除role_permission表中的信息
     * @param req
     * @param resp
     * @throws Exception
     */
    private void deleteByRidAndPid(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        String id = req.getParameter("id");
        permissionService.deleteByRidAndPid(id);

        deleteById(req,resp,id);

    }

    /**
     * 批量删除role_permission表中的信息
     * @param req
     * @param resp
     * @throws Exception
     */
    private void deleteByRidAndPids(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        String ids = req.getParameter("ids");

        permissionService.deleteByRidAndPids(ids);

        deleteByIds(req,resp,ids);

    }


        /**
         * 批量删除权限信息
         * @param req
         * @param resp
         * @throws Exception
         */
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp,String id) throws Exception {
        //获取页面信息
        String ids = req.getParameter("ids");
        //定义一个标记,默认为true
        boolean flag = true;
        //进行判断
        if(flag){
            permissionService.deleteByIds(ids);
        }else{
            //删除失败返回false
            flag = false;
        }
        //将获得的flag类型转为json
        String json = mapper.writeValueAsString(flag);
        //向前端提交json
        resp.getWriter().write(json);

    }

    /**
     * 根据id删除权限信息
     * @param req
     * @param resp
     * @param id
     * @throws Exception
     */
    private void deleteById(HttpServletRequest req, HttpServletResponse resp,String id) throws Exception {
        //实现方法
        permissionService.deleteById(id);
        //再次调用findAll方法
        findAll(req,resp);
    }

    }
